package com.hafu.swing;

import com.hafu.model.Student;
import com.hafu.service.StudentService;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class StudentManagePanel extends JPanel {
    private StudentService studentService;
    private JTable studentTable;
    private DefaultTableModel tableModel;

    public StudentManagePanel(StudentService studentService) {
        this.studentService = studentService;
        setLayout(new BorderLayout());
        initTable();
        loadStudentData();
    }

    private void initTable() {
        String[] columnNames = {"学生ID", "姓名", "性别", "年龄", "专业", "入学日期", "电话", "操作"};
        tableModel = new DefaultTableModel(columnNames, 0);
        studentTable = new JTable(tableModel);
        studentTable.setRowHeight(30);
        // 列宽设置（按需调整）
        studentTable.getColumnModel().getColumn(0).setPreferredWidth(80);
        studentTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        studentTable.getColumnModel().getColumn(2).setPreferredWidth(50);
        studentTable.getColumnModel().getColumn(3).setPreferredWidth(50);
        studentTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        studentTable.getColumnModel().getColumn(5).setPreferredWidth(120);
        studentTable.getColumnModel().getColumn(6).setPreferredWidth(100);
        studentTable.getColumnModel().getColumn(7).setPreferredWidth(100);

        JScrollPane scrollPane = new JScrollPane(studentTable);
        add(scrollPane, BorderLayout.CENTER);
    }

    /**
     * 加载学生数据（带详细日志）
     */
    private void loadStudentData() {
        System.out.println("[StudentManagePanel] 开始加载学生数据");
        tableModel.setRowCount(0); // 清空表格

        List<Student> students = studentService.getAllStudents();
        // 防御性处理：避免 students 为 null
        if (students == null) {
            System.out.println("[警告] studentService 返回 null，改用空列表");
            students = new ArrayList<>();
        }

        System.out.println("[StudentManagePanel] 共获取 " + students.size() + " 条学生数据");

        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println("[加载学生 #" + (i + 1) + "] " + student); // 打印学生对象

            // 严格检查学生属性是否为 null
            if (student.getStudentId() == null) {
                System.out.println("[警告] 学生ID为null，跳过该条数据");
                continue;
            }

            Object[] rowData = {
                    student.getStudentId(),
                    student.getName() != null ? student.getName() : "（姓名为空）",
                    student.getGender() != null ? student.getGender() : "（性别为空）",
                    student.getAge() != null ? student.getAge() : 0, // 年龄为null时设为0
                    student.getMajor() != null ? student.getMajor() : "（专业为空）",
                    student.getEnrollmentDate() != null ? student.getEnrollmentDate() : "（日期为空）",
                    student.getPhone() != null ? student.getPhone() : "（电话为空）",
                    "查看详情"
            };
            tableModel.addRow(rowData);
        }
    }

    public void refreshData() {
        loadStudentData(); // 刷新时重新加载数据
    }

    public void setTableModel(DefaultTableModel tableModel) {
    }

    public void setTable(JTable studentTable) {
    }
}